package org.iteye.bbjava.util.interpreter;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

public class Context {

	Document doc = null;
	String xml;
	/**
	 * transcafor xml string to xml document
	 * 
	 * @param xml
	 *            <code>xml</code>, It is a string of xml;
	 * @return <code>Document type</code>, It is a document of xml;
	 * */
	private Document praseXml() {
		if (null != xml && !"".equals(this.xml)) {
			this.doc = null;
			try {
				doc = DocumentHelper.parseText(xml);
				return doc;
			} catch (DocumentException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}
	
	
	public void setXml(String xml){
		this.xml = xml;
		try {
			this.parseXmlStringToDoc();
		} catch (DocumentException e) {
			throw new RuntimeException("The paramater is not xml");
		}
			
		
	}

	/**
	 * 解释xml串成文档
	 * @author zhang
	 *
	 * */
	@SuppressWarnings("unused")
	private void parseXmlStringToDoc() throws DocumentException{
		
		this.doc =  DocumentHelper.parseText(xml);
	}
	
	/**
	 * 
	 * 
	 * 
	 * */
	public Element getRootElement() {
		this.praseXml();
		return this.doc.getRootElement();
	}

	/**
	 * 
	 * 
	 * 
	 * */
	public Element getElement(String elementName, Element elem) {
		
		Element element = null;
		if (null != elementName && !"".equals(elementName) && null != elem) {
			Node n = elem.getDocument().selectSingleNode("//" + elementName);
		}

		return element;
	}
	
	/**
	 * 通过元素名获得节点
	 * @author zhang
	 * @param elementName 元素名
	 * @param doc xml文档
	 * @return 对应的点节
	 * 
	 * */
	public Node  getNodeByElementName(String elementName, Document doc){
		
		Node node = null;
		if (null != elementName && !"".equals(elementName) && null != doc) {
			node = doc.selectSingleNode("//" + elementName);
		}
		

		return node;
	}
	
	/**
	 * 通过元素名获得节点
	 * @author zhang
	 * @param elementName 元素名
	 * @return 对应的点节
	 * 
	 * */
	public Node getNodeByElementName(String elementName){
		
		return 	this.getNodeByElementName(elementName, this.doc);
	}
	
	
	/**
	 * 
	 * 
	 * 
	 * */
	public Element queryElement(String xpath, Element elem) {
		Element element = null;
		if (null != xpath && !"".equals(xpath) && null != elem) {
			element = elem.element(xpath);
		}

		return element;
	}
	
	/**
	 * 
	 * 
	 * 
	 * */
	public Element queryElement(String xpath, Document doc) {
		Element element = null;
		if (null != xpath && !"".equals(xpath) && null != doc) {
			element = (Element)doc.selectSingleNode(xpath);
		}

		return element;
	}
	
	/**
	 * 根据xpath 在xml文档中找对应的节点
	 * @author zhang
	 * @param xpath xml 节点路径
	 * @param doc xml文档
	 * @return 返回在doc中路径为xpath的节点
	 * */
	public Node queryNode(String xpath, Document doc){
		
		Node node  = null;
		if (null != xpath && !"".equals(xpath) && null != doc) {
			node = doc.selectSingleNode(xpath);
		}
		return node;
	}
	
	/**
	 * 根据xpath 在xml文档中找对应的节点
	 * @author zhang
	 * @param xpath xml 节点路径
	 * @param doc xml文档
	 * @return 返回在doc中路径为xpath的节点
	 * */
	public List<Node> queryNodes(String xpath, Document doc){
		
		List<Node> nodes  = null;
		if (null != xpath && !"".equals(xpath) && null != doc) {
			nodes = doc.selectNodes(xpath);
		}
		return nodes;
	}
	
	/**
	 * 根据xpath 在xml文档中找对应的节点
	 * @author zhang
	 * @param xpath xml 节点路径
	 * @return 返回路径为xpath的节点
	 * */
	public List<Node> queryNodes(String xpath){
		
		return this.queryNodes(xpath, this.doc);
	}
	/**
	 * 根据xpath 在xml文档中找对应的节点
	 * @author zhang
	 * @param xpath xml 节点路径
	 * @return 返回路径为xpath的节点
	 * */
	public Node queryNode(String xpath){
		
		return this.queryNode(xpath, this.doc);
	}
	
	/**
	 * 根据xpath串查找元素
	 * @author zhang
	 * @param xpath 
	 * @return 对应的元素 
	 * 
	 * */
	public Element queryElement(String xpath){
		
		return this.queryElement(xpath, this.doc);
	}
	

	public List<Element> getElements(String elementName, Element elem) {
		List<Element> elementList = null;
		if (null != elementName && !"".equals(elementName) && null != elem) {
			elementList = elem.elements("//" + elementName);
		}
		return elementList;
	}

	/**
	 * 
	 * 
	 * 
	 * */
	public String getText(Element elem) {
		String text = null;
		if (null != elem) {
			text = elem.getTextTrim();
			return text;
		}
		return text;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}

	
}
